﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="user_pay_inc.aspx.cs" Inherits="newBlog.CenterInc.view_inc.user_pay_inc" %>
<%@ Import Namespace="newBlog.CenterInc.Ctrltor" %>
<%@ Import Namespace="newBlog.CenterInc.tool" %>
<%

    Response.ContentType = "application/json";
    if (Session["user_id"]==null) {
        Response.Write("{\"error\":\"您未登录禁止操作\"}");
        return;
    }
    int mode = Convert.ToInt32(Request.QueryString["mode"]);//1文章购买，2积分充值
    string user_id = Session["user_id"].ToString();
    Article_inc article_Inc = new Article_inc();
    Pay_inc pay_Inc = new Pay_inc();
    User_inc user_Inc = new User_inc();
    isString isString = new isString();
    int price=0;
    switch (mode) {
        case 1:
            string article_id = Request.Form["article_id"];

            price = article_Inc.getPrice(article_id);//获取文章的价格
            int balance = user_Inc.getBlance(user_id);//获取用户余额
            if (balance<price) {
                Response.Write("{\"error\":\"您的积分余额不足\"}");
                return;
            }
            if (pay_Inc.CreateOrder("文章id_"+article_id,user_id,price)) {
                Response.Write("{\"error\":\"订单创建失败\"}");
                return;
            }
            if (pay_Inc.Deduction(user_id,price)) {
                Response.Write("{\"error\":\"积分支付失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"购买成功\"}");
            return;
        case 2:
            string key = Request.Form["key"];
            string value = Request.Form["value"];
            if (isString.isEmpty(key,value)) {
                Response.Write("{\"error\":\"卡密以及提取码不能为空\"}");
                return;
            }
            if (pay_Inc.isKeyValueUse(key,value)) {
                Response.Write("{\"error\":\"卡密已被人使用\"}");
                return;
            }
            price = pay_Inc.getKeyValuePrice(key,value);
            if (pay_Inc.CreateRecharge(user_id,price,"卡密："+key)) {
                Response.Write("{\"error\":\"创建订单失败\"}");
                return;
            }
            if (pay_Inc.modifyKeyValueState(user_id,key,value)) { 
                Response.Write("{\"error\":\"卡密状态修改失败\"}");
                return;
            }
            if (pay_Inc.increase(user_id,price)) {
                Response.Write("{\"error\":\"更新余额失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"充值成功\"}");
            return;
    }
    %>
